//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by 周小伟 on 16/8/25.
//  Copyright © 2016年 周小伟. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    init(){
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        print(page)
        if page == imageCount - 1 {
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell{
                cell.startAnimation()
            }
        }
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    

}

class NewFeatureCell: UICollectionViewCell {
    var index:Int = 0{
        didSet{
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    private func start(){
        print("用我吧")
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
    }
    
    private func startAnimation(){
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) in
                print("OK")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        iconImage.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        startButton.addTarget(self, action: Selector(start()), forControlEvents: .TouchUpInside)
    }
    
    private lazy var iconImage:UIImageView = UIImageView()
    
    private lazy var startButton:UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
}
